/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.allerj.task;

import it.allerj.gui.component.MyProgressBar;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingWorker;

/**
 *
 * @author Alessandro Veracchi
 */
public class ScanBarcodeTask extends SwingWorker<Void, Void> {
        private java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("it/allerj/bundle/allerj");
        private MyProgressBar  myProgressBar;
        private JButton forward;
        private JLabel result;
        private JFrame mainJFrame;
        public ScanBarcodeTask(MyProgressBar myProgressBar, JFrame mainJFrame, JButton forward, JLabel result){
            super();
            this.myProgressBar = myProgressBar;
            this.forward = forward;
            this.result = result;
            this.mainJFrame = mainJFrame;
        }
        private ScanBarcodeTask(){}
        @Override
        public Void doInBackground() {
            Random random = new Random();
            int progress = 0;
            //Initialize progress property.
            setProgress(0);
            while (progress < 100) {
                //Sleep for up to one second.
                try {
                    Thread.sleep(random.nextInt(100));
                } catch (InterruptedException ignore) {}
                //Make random progress.
                progress += random.nextInt(100);
                setProgress(Math.min(progress, 100));
            }
            return null;
        }

        /*
         * Executed in event dispatching thread
         */
        @Override
        public void done() {
            if(myProgressBar!=null){
                myProgressBar.getStartButton().setEnabled(true);
            }
            if(forward!=null && result!=null && mainJFrame!=null){
                mainJFrame.setCursor(null); //turn off the wait cursor
                forward.setEnabled(true);
                result.setText(bundle.getString("i18n.recived.barcode")+" "+12345);
            }
        }
    }
